<?php
namespace app\sys\model;
use think\Validate;
use think\Db;
use app\base\model\Base;
class SlideM extends Base
{

    protected function _V($scene,$data)
    {
        $rule = [
            'type' => 'require|number',
            'name' => 'require',
            'linksrc' => 'require|url',
            'sort_num' => 'require|number',
        ];
        $msg = [];
        $field = [
            'type'=>'轮播图分类',
            'name'=>'轮播图名称',
            'linksrc'=>'链接地址',
            'sort_num'=>'排序',
        ];
        $validate = new Validate($rule,$msg,$field);
        $validate->scene('add', ['type','name','linksrc','sort_num']);
        $validate->scene('edit', ['type','name','linksrc','sort_num']);
        $result = $validate->scene($scene)->check($data);
        if($result === false)
        {
            return $validate->getError();
        }
        else
        {
            return true;
        }

    }

    /**
     * 新增数据
     * @return mixed
     */

    public function add($data)
    {
        //验证数据
        $res = $this->_V('add',$data);
        if($res !== true)
        {
            $this->code = -1;
            $this->msg = $res;
            return false;
        }
        $insertData = [];

        //处理图片
        $ret = uploadOne('pic', array(
            array(800, 800),
            array(300, 300),
            array(100, 100),
        ));
        if($ret['ok'] == 1)
        {
            $insertData['pic'] = toQiNiu($ret['images'][0]);   // 原图地址
            $insertData['big_pic'] = toQiNiu($ret['images'][1]);   // 第一个缩略图地址
            $insertData['mid_pic'] = toQiNiu($ret['images'][2]);   // 第二个缩略图地址
            $insertData['sm_pic'] = toQiNiu($ret['images'][3]);   // 第三个缩略图地址
        }
        else if($ret['ok'] == 2)
        {
            $this->code = -1;
            $this->msg = '图片必须上传';
            return false;
        }
        else
        {
            $this->code = -1;
            $this->msg = $ret['error'];
            return false;
        }
        //判断用户
        $insertData['admin_id'] = session('admin_id');
        $insertData['type'] = $data['type'];
        $insertData['name'] = $data['name'];
        $insertData['linksrc'] = $data['linksrc'];
        $insertData['sort_num'] = $data['sort_num'];
        $id = Db::table('sys_slide')->insertGetId($insertData);
        if(!$id)
        {
            $this->code = -1;
            $this->msg = '新增失败';
            return false;
        }
        return true;
    }

    /**
     * 全部数据
     */
    public function lst($page = 15)
    {
        $data = Db::table('sys_slide')->where('is_usable',1)->field('id,type,name,sm_pic,linksrc')->paginate($page);
        return $data;
    }

    /**
     * 修改数据
     * @param $data
     * @return bool
     */
    public function edit($data)
    {
        $res = $this->_V('edit',$data);
        if($res !== true)
        {
            $this->code = -1;
            $this->msg = $res;
            return false;
        }
        $insertData = [];

        //处理图片
        $ret = uploadOne('pic', array(
            array(800, 800),
            array(300, 300),
            array(100, 100),
        ));
        if($ret['ok'] == 1)
        {
            $insertData['pic'] = toQiNiu($ret['images'][0]);   // 原图地址
            $insertData['big_pic'] = toQiNiu($ret['images'][1]);   // 第一个缩略图地址
            $insertData['mid_pic'] = toQiNiu($ret['images'][2]);   // 第二个缩略图地址
            $insertData['sm_pic'] = toQiNiu($ret['images'][3]);   // 第三个缩略图地址
        }
        else if($ret['ok'] == 2)
        {
        }
        else
        {
            $this->code = -1;
            $this->msg = $ret['error'];
            return false;
        }
        //判断用户
        $insertData['type'] = $data['type'];
        $insertData['name'] = $data['name'];
        $insertData['linksrc'] = $data['linksrc'];
        $insertData['sort_num'] = $data['sort_num'];
        $id = Db::table('sys_slide')->where('id',$data['id'])->update($insertData);
        if(!$id)
        {
            $this->code = -1;
            $this->msg = '修改失败';
            return false;
        }
        return true;
    }

    /**
     * 删除数据
     * @param $id
     */
    public function del($id)
    {
        return Db::table('sys_slide')->where('id',$id)->update(['is_usable'=>0]);
    }
}
